/*
 * CoordinateInfoPanel.java
 *
 * Created on July 1, 2005, 3:05 PM
 */

package ika.gui;

import ika.map.tools.*;
import ika.map.*;
import ika.utils.CoordinateFormatter;
import java.text.*;
import ika.geo.*;

/**
 *
 * @author  jenny
 */
public class GeoObjectInfoPanel extends javax.swing.JPanel
        implements MapToolMouseMotionListener {
    
    private String geoSetName;
    
    /** Creates new form CoordinateInfoPanel */
    public GeoObjectInfoPanel() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        nameTextLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        nameTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        nameTextLabel.setText("Point:");
        nameTextLabel.setToolTipText("Name of the point under the mouse pointer.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(nameTextLabel, gridBagConstraints);

        nameLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        nameLabel.setText("-");
        nameLabel.setToolTipText("Name of the point under the mouse pointer.");
        nameLabel.setPreferredSize(new java.awt.Dimension(120, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 10);
        add(nameLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    private void updateName(java.awt.geom.Point2D.Double point,
            ika.gui.MapComponent mapComponent) {
        
        String name = "-";
        
        if (point != null) {
            GeoSet geoSet = mapComponent.getGeoSet();
            // search for named GeoSet in passed MapComponent
            if (this.geoSetName != null &&
                    !this.geoSetName.equals(geoSet.getName())) {
                GeoObject geoObj = geoSet.getGeoObject(this.geoSetName);
                if (geoObj instanceof GeoSet)
                    geoSet = (GeoSet)geoObj;
            }
            
            double scale = mapComponent.getScaleFactor();
            double tolDist = 0;
            GeoObject obj = geoSet.getObjectAtPosition(point, tolDist, scale,
                    false, true);
            if (obj != null)
                name = obj.getName();
            if (name == null || name.equals(""))
                name = "unnamed";
        }
        this.nameLabel.setText(name);
    }
    
    public void mouseMoved(java.awt.geom.Point2D.Double point,
            ika.gui.MapComponent mapComponent) {
        this.updateName(point, mapComponent);
    }
    
    public void registerWithMapComponent(MapComponent mapComponent) {
        mapComponent.addMouseMotionListener(this);
    }

    public String getGeoSetName() {
        return geoSetName;
    }

    public void setGeoSetName(String geoSetName) {
        this.geoSetName = geoSetName;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel nameTextLabel;
    // End of variables declaration//GEN-END:variables
    
}
